package Tcp;

import android.os.Handler;
import android.os.Message;
import android.text.TextUtils;
import android.util.Log;
import android.widget.Toast;

import java.io.DataInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.net.InetAddress;
import java.net.Socket;

public class Tcp_Client extends Thread {

    private String TAG = "TcpClient";

    private PrintWriter pw;
    private InputStream is;
    private DataInputStream dis;
    private boolean isRun = true;
    private Socket socket = null;
    byte buff[]  = new byte[1024];
    private String rcvMsg;
    private int rcvLen;

    private Handler mhandler;

    private boolean isruning;
    public InputStream inputStream;
    public OutputStream outputStream;
    private InetAddress inetAddress;                             //IP地址
    private int port;                                            //端口号
    public static int  CLIENT_STATE_CORRECT_READ=7;
    public static int  CLIENT_STATE_CORRECT_WRITE=8;               //正常通信信息
    public static int  CLIENT_STATE_ERROR=9;                 //发生错误异常信息
    public static int  CLIENT_STATE_IOFO=10;



    public Tcp_Client(Handler mhandler){
        this.mhandler=mhandler;
        isruning=true;
    }

    public void setInetAddress(InetAddress inetAddress) {
        this.inetAddress = inetAddress;
    }
    public void setPort(int port) {
        this.port = port;
    }



    public  void close(){
        if (socket !=null){
            try {
                socket.close();
                socket=null;
                isruning=false;
            } catch (IOException e) {
            }
        }else if (socket ==null){
            Log.e("dheuha", "未建立连接");
        }
    }
    public void getadress(){
        String[] strings = new String[2];
        strings[0]=socket.getInetAddress().getHostAddress();
        strings[1]=socket.getInetAddress().getHostName();
        Message message = mhandler.obtainMessage(CLIENT_STATE_IOFO,-1,-1,strings);
        mhandler.sendMessage(message);
    }
    @Override
    public void run() {

        try {
            socket=new Socket(inetAddress,port);

            new Receive_Thread(socket).start();
            getadress();

        } catch (IOException e) {
            senderror();
        }
    }
    class Receive_Thread extends Thread{
        StringBuffer str = new StringBuffer();
        String weight = "";
        private  Socket msocket;
        public Receive_Thread (Socket msocket){
            this.msocket =msocket;
        }

        @Override
        public void run() {
            while (isruning){
                try {
                    pw = new PrintWriter(msocket.getOutputStream(), true);
                    is = msocket.getInputStream();
                    dis = new DataInputStream(is);
                    rcvLen = dis.read(buff);
                     if (rcvLen==-1){
                         if(msocket!=null){
                             try {
                                 msocket.close();
                             } catch (IOException e) {
                                 e.printStackTrace();
                             }
                             Log.e(TAG, "未建立连接");
                         }
                     }else{
                         rcvMsg = new String(buff,0,rcvLen,"utf-8");
                     }

                    Log.i("dwwdeexs", "run: 收到消息:"+ rcvMsg);
                    if (rcvMsg.equals("QuitClient")){   //服务器要求客户端结束
                        isRun = false;
                    }
                    if (!TextUtils.isEmpty(rcvMsg)){
                        mhandler.sendMessage(mhandler.
                                obtainMessage(CLIENT_STATE_CORRECT_READ,rcvMsg));
                    }else{
                        senderror();
                    }


                } catch (IOException e) {
                    senderror();
                }

            }

            }


    }
    void senderror(){
        mhandler.sendMessage(mhandler.obtainMessage(CLIENT_STATE_ERROR));
    }
}
